 require(["common", "queryString", "coAjax", "hash_routie", "serverNotify", "href", './js/tools/getarea.js'], function(jSouper, QueryString, coAjax, hash_routie, serverNotify, href) {
 	//订单取消的触发事件
 	App.set("$Event.cancel_ach_success", function(vm, result) {
 		href.jump("/catering.main.html");
 	});

 	function _set_user_pay_ach(pay_ach) {
 		App.set("user.pay_ach", pay_ach);
 	};
 	hash_routie.on('pay', function() {
 		var queryString = new QueryString(location.hash);
 		var ach_id = queryString.get("ach_id");
 		if (!ach_id) {
 			alert("error", "无效的订单编号");
 			href.jump('catering.main.html');
 		}
 		//获取订单信息
 		coAjax.get(appConfig.user.ach_by_id + ach_id, function(result) {
 			_set_user_pay_ach(result.result);
 			//App.set('aa', result.result);
 		}, function(errorCode) {
 			console.log("errorCode:", errorCode);
 			alert("error", "无效的订单编号");
 		});
 		//支付
 		coAjax.get(appConfig.user.ach_by_id + ach_id, function(result) {
 			var pay = result.result;
 			var price = pay.cash;
 			var telpay = "http://www.sportyuan.com/tenpay/tenpay.php?order_no=" + ach_id + "&product_name=商品&order_price=" + price + "&trade_mode=1&remarkexplain=商品内容";
 			App.set('telpay', telpay);
 		});
 		//获取可抵用的额度
 		coAjax.get(appConfig.user.bus_bind_by_id, {
 			bus_id: appConfig.bus_id
 		}, function(result) {
 			App.set("$Cache.bus_bind", result.result);
 		});
 		//获取餐桌列表
 		coAjax.get(appConfig.space.list_by_type + appConfig.bus_id, {
 			type: appConfig.SPACE_TYPE["桌号"]
 		}, function(result) {
 			App.set("$Cache.pay.space_seat.list", result.result);
 		});
 	});

 	App.set("$Event.pays.change_pay_method", function(e) {
 		var pay_method = App.get("$Cache.pays.buy_method");
 		coAjax.put(appConfig.user.ach_change_pay_method + App.get("user.pay_ach._id"), {
 			pay_method: pay_method
 		}, function(result) {
 			_set_user_pay_ach(result.result);
 			alert("success", "支付方式修改成功");
 		}, function(errorCode, xhr, errorMsg) {
 			//数据还原
 			App.getModel().touchOff("user.pay_ach.info.pay_method");
 			alert("error", errorMsg);
 		});
 	});
 	//获取用户地址列表
 	coAjax.get(appConfig.user.address_detail, function(result) {
 		App.set("user.address_detail", result.result);
 		App.getModel().touchOff("user.pay_ach.info.user_address");
 	});

 	// 添加地址
 	App.set("$Event.add_user_address", function() {
 		var new_user_address = {
 			province: $("[name='province']").val(),
 			town: $("[name='city']").val(),
 			county: $("[name='area']").val(),
 			// street:
 			detail: $("[name='detailed_addre']").val(),
 			mobilenumber: $("[name='mobile_num']").val(),
 			phonenumber: $("[name='phone_num']").val(),
 			postcode: $("[name='zip_code']").val(),
 			name: $("[name='name']").val()
 		};
 		coAjax.post(appConfig.user.add_address, new_user_address, function(result) {
 			var address_detail = App.get("user.address_detail");
 			address_detail.unshift(result.result);
 			console.log(result.result);
 			App.set("user.address_detail", address_detail);
 			App.getModel().touchOff("user.pay_ach.info.user_address");
 			alert("success", '地址添加成功');
 			coAjax.put(appConfig.user.ach_change_user_address + App.get("user.pay_ach._id"), {
 				user_address_id: result.result._id
 			}, function(result) {
 				alert("success", "收获地址修改成功");
 				App.set("user.pay_ach.info.user_address", result.result);
 			}, function(errorCode, xhr, errorMsg) {
 				App.getModel().touchOff("user.pay_ach.info.user_address._id");
 				alert("error", errorMsg);
 			});
 		});
 	});
 	//改变订单使用的收获地址
 	App.set("$Event.pays.change_user_address", function(e, vm) {
 		coAjax.put(appConfig.user.ach_change_user_address + App.get("user.pay_ach._id"), {
 			user_address_id: vm.get("_id")
 		}, function(result) {
 			alert("收获地址修改成功");
 			App.set("user.pay_ach.info.user_address", result.result);
 		}, function(errorCode, xhr, errorMsg) {
 			App.getModel().touchOff("user.pay_ach.info.user_address._id");
 			alert("error", errorMsg);
 		});
 	});
 	/*
 	 * Sock连接
 	 */
 	var user_notify = serverNotify("user");
 	user_notify.on("pay_success", function(new_ach_info) {
 		_set_user_pay_ach(new_ach_info);
 		href.jump("/personal-beta.html");
 	});
 	user_notify.on("ach.state.changed", function(ach_info) {
 		if (App.get("user.pay_ach._id") == ach_info.ach_id) {
 			App.set("user.pay_ach.info.state", ach_info.state);
 		}
 	});
 	user_notify.on("bus.space.use_state.changed", function(space) {
 		if (appConfig.bus_id == space.bus_id) {
 			var space_seat_list = App.get("$Cache.pay.space_seat.list");
 			space_seat_list = space_seat_list.map(function(space_info) {
 				if (space_info._id == space._id) {
 					return space
 				} else {
 					return space_info
 				}
 			});
 			App.set("$Cache.pay.space_seat.list", space_seat_list);
 		}
 	});

 	//绑定优惠码
 	App.set("$Event.pay_ach.bind_coupon", function(e) {
 		coAjax.put(appConfig.user.ach_bind_coupon, {
 			ach_id: App.get("user.pay_ach._id"),
 			coupon_id: App.get("$Cache.pay_ach.coupon_id")
 		}, function(result) {
 			alert("优惠码绑定成功");
 			_set_user_pay_ach(result.result);
 		});
 	});
 	//使用抵扣金额
 	App.set("$Event.pay_ach.bind_acc", function(e) {
 		coAjax.put(appConfig.user.ach_bind_acc, {
 			ach_id: App.get("user.pay_ach._id"),
 			bind_acc: App.get("$Cache.pay_ach.bind_acc")
 		}, function(result) {
 			alert("抵扣成功");
 			_set_user_pay_ach(result.result);
 		});
 	});

 	App.set("$Event.pays.change_use_space", function() {
 		coAjax.put(appConfig.user.ach_change_use_space + App.get("user.pay_ach._id"), {
 			use_space: !App.get("user.pay_ach.info.use_space")
 		}, function(result) {
 			alert("就餐形式修改成功");
 			_set_user_pay_ach(result.result);
 		});
 	});
 	App.set("$Event.pays.change_space", function() {
 		coAjax.put(appConfig.user.ach_change_space + App.get("user.pay_ach._id"), {
 			space_id: App.get("user.pay_ach.info.space_id")
 		}, function(result) {
 			alert("餐桌号修改成功");
 			_set_user_pay_ach(result.result);
 		}, function(errorCode, xhr, errorMsg) {
 			var errorMsgMap = {
 				"无效的空间编号": "桌位号无效",
 				"未指定用户使用空间": "未指定用户的用餐桌号",
 				"指定空间不属于当前商家": "桌位号无效",
 				"参数错误":"请选有效餐桌"
 			};
 			errorMsg = errorMsgMap[errorMsg] || errorMsg;
 			alert("error", errorMsg);
 			App.model.touchOff("user.pay_ach");
 		})
 	});
 });
 //errorMsg:
 /*
 无效的空间编号:桌位号无效
 未指定用户使用空间:未指定用户的用餐桌号
 指定空间不属于当前商家:桌位号无效
 */